<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入系统配置名"
        v-model="queryForm.paramName"
      >
      </el-input>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'sys:add'"
        class="filter-item"
        style="margin-left: 10px;"
        @click="openCreateDialog(null)"
        type="success"
        icon="el-icon-edit"
        >新增</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column
        prop="paramName"
        label="配置名"
        width="450"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="paramValue"
        label="配置值"
        width="450"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="paramDesc" label="描述" width="450" align="center">
      </el-table-column>
      <el-table-column
        width="300"
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'sys:update'"
            @click="editParam(scope.row)"
            type="primary"
            size="mini"
            icon="el-icon-edit"
            circle
            title="编辑"
          ></el-button>
          <el-button
            v-permission="'sys:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`${editParamForm.rowId ? '编辑' : '新增'}字典`"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        :model="editParamForm"
        label-width="100px"
        :rules="editParamFormRules"
        ref="editParamForm"
      >
        <el-form-item label="配置组:" prop="paramGroup">
          <el-select
            v-model="editParamForm.paramGroup"
            placeholder="请选择配置组"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.PARAM_GROUP"
              :key="key"
              :label="val"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置名:" prop="paramName">
          <el-input
            v-model="editParamForm.paramName"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="配置值:" prop="paramValue">
          <el-input
            v-model="editParamForm.paramValue"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="paramDesc">
          <el-input
            v-model="editParamForm.paramDesc"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="为空默认值:" prop="optional">
          <el-input
            v-model="editParamForm.optional"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="线上修改?:" prop="changeOnline">
          <el-select
            v-model="editParamForm.changeOnline"
            placeholder="请选择是否支持线上修改"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.CHANGE_ONLINE"
              :key="key"
              :label="val"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示顺序:" prop="orderId">
          <el-input
            v-model="editParamForm.orderId"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="配置分类:" prop="groupId">
          <el-input
            v-model="editParamForm.groupId"
            auto-complete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditParam">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryParam, deleteParam, addOrEditParam } from '@/api/system'

export default {
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      editParamForm: {
        rowId: undefined,
        paramGroup: '',
        paramName: '',
        paramValue: '',
        paramDesc: '',
        optional: '',
        changeOnline: '',
        orderId: '',
        groupId: ''
      },
      editParamFormRules: {
        paramName: [
          { required: true, message: '请输入配置名', trigger: 'blur' }
        ],
        paramValue: [
          { required: true, message: '请输入配置值', trigger: 'change' }
        ]
      },
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        paramName: ''
      },
      disabled: false,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryParam(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteParam(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    editParam({
      rowId,
      paramGroup,
      paramName,
      paramValue,
      paramDesc,
      optional,
      changeOnline,
      orderId,
      groupId
    }) {
      this.openCreateDialog(rowId)
      this.editParamForm = {
        rowId: rowId,
        paramGroup: paramGroup,
        paramName: paramName,
        paramValue: paramValue,
        paramDesc: paramDesc,
        optional: optional,
        changeOnline: changeOnline,
        orderId: orderId,
        groupId: groupId
      }
    },
    openCreateDialog(rowId) {
      this.editParamForm = {}
      this.dialogFormVisible = true
      this.$refs.editParamForm && this.$refs.editParamForm.resetFields()
      this.disabled = rowId != null
    },
    addOrEditParam() {
      this.$refs.editParamForm.validate(valid => {
        if (valid) {
          addOrEditParam(this.editParamForm).then(
            ({ data: { data, msg, code } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
